package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.model.Task;
import ge.telasi.tasks.model.TaskReceiver;
import ge.telasi.tasks.model.validation.TaskValidator;
import ge.telasi.tasks.ui.log.Loggable;
import ge.telasi.tasks.ui.log.Logger;
import ge.telasi.tasks.ui.log.LoggerUtils;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JPanel;

/**
 * @author dimitri
 */
public class NewTaskReceiverPanel extends JPanel implements Loggable {

    public NewTaskReceiverPanel() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        pnAssignees = new ge.telasi.tasks.ui.task.TaskReceiverEditPanel();
        jLabel2 = new javax.swing.JLabel();
        pnCopies = new ge.telasi.tasks.ui.task.TaskReceiverEditPanel();
        jSeparator1 = new javax.swing.JSeparator();

        jLabel1.setText("შესრულება დაევალოს:");

        pnAssignees.setName("Task.assignees"); // NOI18N

        jLabel2.setText("ასლი გაეგზავნოს:");

        pnCopies.setName("Task.copies"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(pnAssignees, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE)
                    .addComponent(pnCopies, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 563, Short.MAX_VALUE)
                .addGap(6, 6, 6))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(pnAssignees, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(pnCopies, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSeparator jSeparator1;
    private ge.telasi.tasks.ui.task.TaskReceiverEditPanel pnAssignees;
    private ge.telasi.tasks.ui.task.TaskReceiverEditPanel pnCopies;
    // End of variables declaration//GEN-END:variables

    private Logger logger;

    public Logger getLogger() {
        return logger;
    }

    public void setLogger(Logger logger) {
        this.logger = logger;
    }

    public JComponent[] allFields() {
        return new JComponent[] { pnAssignees, pnCopies };
    }

    public List<TaskReceiver> getReceivers() {
        List<TaskReceiver> receivers = new ArrayList<TaskReceiver>();
        List<TaskReceiver> assignees = pnAssignees.getReceivers();
        List<TaskReceiver> copies = pnCopies.getReceivers();
        for (TaskReceiver assignee : assignees) {
            assignee.setCopy(false);
            receivers.add(assignee);
        }
        for (TaskReceiver copy : copies) {
            copy.setCopy(true);
            receivers.add(copy);
        }
        return receivers;
    }

    protected void clearLogger() {
        LoggerUtils.clearLogger(logger, allFields());
    }
    
    protected boolean validateForm() {
        clearLogger();
        try {
            Task task = new Task();
            fillTask(task);
            new TaskValidator().validateAssignees(task.getAssignees());
            return true;
        } catch (Exception ex) {
            LoggerUtils.manageException(this, logger, ex, allFields());
            return false;
        }
    }

    public void fillTask(Task task) {
        task.setReceivers(getReceivers());
    }

    public void setAssignees(List<TaskReceiver> assignees) {
        pnAssignees.setReceivers(assignees);
    }

    public void setCopies(List<TaskReceiver> copies) {
        pnCopies.setReceivers(copies);
    }

}
